package com.example.mobile2021606176mhq0511;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private ImageView ivMainBanner;
    private TextView tvMainContent;

    final String IMAGEPTH="http://192.168.48.81:3000/";
    final String TEXTPATH="http://192.168.48.81:3000/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        new Thread(){
            public void run(){
                super.run();
            try{
                URL imageUrl = new URL(IMAGEPTH);
                HttpURLConnection imageConnection = (HttpURLConnection) imageUrl.openConnection();
                imageConnection.setRequestMethod("GET");
                imageConnection.setConnectTimeout(5000);

                InputStream imageIS = imageConnection.getInputStream();
                final Bitmap bitmap = BitmapFactory.decodeStream(imageIS);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ivMainBanner.setImageBitmap(bitmap);
                    }
                });
                imageIS.close();
                imageConnection.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }
            }
        }.start();
        new Thread(){
            public void run(){
                super.run();

            }
        }.start();
        try {
            URL textUrl = new URL(TEXTPATH);
            HttpURLConnection textConnection = (HttpURLConnection) textUrl.openConnection();
            InputStream textIs = textConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(textIs);
            final BufferedReader br = new BufferedReader(isr);
            String line = null;
            String temp = "";
            while ((line = br.readLine()) != null) {
                temp += line;
                temp += "/n";

            }
            final String msg = temp;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvMainContent.setText(msg);
                }
            });
            br.close();
            isr.close();
            textIs.close();
            textConnection.disconnect();
        }catch (Exception e){
            e.printStackTrace();



        }
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        ivMainBanner = (ImageView) findViewById(R.id.ivMainBanner);
        tvMainContent = (TextView) findViewById(R.id.tvMainContent);
    }
}